package com.ship.boats.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoatsCanalAccess;
import com.ship.boats.domain.SpBoatsWeather;
import com.ship.boats.domain.dto.QueryCanalMessageRq;
import com.ship.boats.domain.vo.QueryCanalMessageInfo;
import com.ship.boats.service.BoatsCanalAccessService;
import com.ship.boats.service.BoatsVoyageMessageService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.web.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>BoatsCanalMessageController</b>
 *
 * @description: BoatsCanalMessageController <br>
 * @date: 2024/4/18 10:06 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "运河信息")
@RestController
public class BoatsCanalMessageController {
    private static final Logger logger = LoggerFactory.getLogger(BoatsCanalMessageController.class);

    @Autowired
    private BoatsCanalAccessService boatsCanalAccessService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @Autowired
    private BoatsVoyageMessageService boatsVoyageMessageService;

    @ApiOperation("测试同步运河通行量")
    @PostMapping("/get/canalAccess/test")
    public void getCanalAccessTest(){
        boatsCanalAccessService.addCanalAccessList();
    }

    @ApiOperation("分页查询运河信息列表")
    @GetMapping("/query/canalMessage/list")
    public JsonResult queryCanalMessageList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                            @RequestParam(required = false) String canalName){
        PageInfo<SpBoatsCanalAccess> pageInfo = boatsCanalAccessService.queryCanalMessageList(pageNum,pageSize,canalName);
        logger.info("分页查询运河信息列表返回结果:{}"+ JSONUtil.parse(pageInfo));
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("根据运河名称查询运河详情")
    @PostMapping("/query/canalMessage/info")
    public JsonResult queryCanalMessageInfo(@RequestBody QueryCanalMessageRq rq){
        PageInfo<QueryCanalMessageInfo> pageInfo = boatsCanalAccessService.queryCanalMessageInfo(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出运河信息列表")
    @PostMapping("/export/canalMessage")
    public JsonResult exportCanalMessage(@RequestBody QueryCanalMessageRq rq){
        List<QueryCanalMessageInfo> canalAccessList = boatsCanalAccessService.exportCanalMessage(rq);
        Field[] fields = QueryCanalMessageInfo.class.getDeclaredFields();
        List<String> tableList = new ArrayList<>();
        tableList.add("createDate");
        tableList.add("passNum");
        tableList.add("avgPassTime");
        tableList.add("lngPassNum");
        tableList.add("lngAvgPassTime");
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("日期");
        tableNameList.add("日通行量（艘）");
        tableNameList.add("平均通行时间（小时）");
        tableNameList.add("LNG船只通行量(艘)");
        tableNameList.add("LNG船只平均通行时间(小时)");
        String url = exportDownloadUtil.exportUtil(canalAccessList,fields,
                tableList,tableNameList,
                rq.getCanalName()+"信息列表");
        return JsonResult.success(url);
    }

}
